<?php
/**
 * Created by PhpStorm.
 * User: 70767
 * Date: 2020/5/6
 * Time: 23:34
 */

namespace app\api\controller;

use app\common\logic\StorehouseLogic;
use think\Db;
use think\Validate;

/**
 * 仓库相关接口
 * Class Storehouse
 * @package app\index\controller
 */
class Storehouse extends Base
{

    /*
    * 初始化操作
    */
    public function _initialize()
    {
        $this->nocheckArr = ['get_config', 'get_storehouse_list', 'get_storehouse_msg','get_nearest'];
        parent::_initialize();
    }

    /**
     * 获得配置文件
     */
    public function get_config(){
        $house_logic = new StorehouseLogic();
        $rs = $house_logic->getConfig();
        $this->returnMsg(1, '', $rs);
    }

    /**
     * 获得最近的仓库
     */
    public function get_nearest(){
        $long = input('long',0);
        $lat = input('lat',0);

        $house_logic = new StorehouseLogic();
        $rs = $house_logic->get_nearest($long,$lat);
        $this->returnMsg(1, '', $rs);
    }

    /**
     * 获得仓库列表
     */
    public function get_storehouse_list()
    {
        //区域
        //城市
        //类型
        $select_info = input('post.');
        $region_id = input('region_id', 0);
        $city_id = input('city_id', 0);
        $show_type = input('show_type', 1);
        $rent_status = input('rent_status', 0);

        $rule = [
            'region_id' => 'number',
            'city_id' => 'number',
            'show_type' => 'number',
        ];

        $msg = [
            'region_id.require' => '区域格式错误',
            'city_id.number' => '城市格式错误',
            'show_type.number' => '格式错误'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($select_info);
        if (!$result) {
//            return ['status' => '-1', 'msg' => $validate->getError()];
            $this->returnMsg(-1, $validate->getError(), []);
        }

        $house_logic = new StorehouseLogic();
        $rs = $house_logic->store_house_list($region_id, $city_id, $show_type,$rent_status);
        $this->returnMsg(1, '', $rs);
    }

    /**
     * 获得仓库详情
     */
    public function get_storehouse_msg(){
        $house_id = input('house_id', 0);
        $rule = [
            'house_id' => 'number'
        ];

        $msg = [
            'house_id.require' => '请输入正确的仓库id'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check(['house_id'=>$house_id]);
        if (!$result) {
//            return ['status' => '-1', 'msg' => $validate->getError()];
            $this->returnMsg(-1, $validate->getError(), []);
        }

        $house_logic = new StorehouseLogic();
        $rs = $house_logic->get_storehouse_msg($house_id);
        $this->returnMsg(1, '', $rs);
    }
}